package com.example.testapplication.Paging;

import androidx.annotation.NonNull;
import androidx.paging.PageKeyedDataSource;

import java.util.ArrayList;
import java.util.List;

public class PagingDataSource extends PageKeyedDataSource<Integer,Income> {
    public static final int PAGE_SIZE = 10;
    // 加载初始页的
    @Override
    public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, Income> callback) {
        callback.onResult(getIncome(1),null,2);

    }


    @Override
    public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Income> callback) {
        if(params.key == 2){
            callback.onResult(getIncome(2),3);
            return;
        }
        callback.onResult(getIncome(3),null);

    }

    @Override
    public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, Income> callback) {

    }

    public List<Income> getIncome(int page){
        List<Income> incomes = new ArrayList<>();
        // 1-9  10-19
        for(int i=(page -1) *PAGE_SIZE;i < page*PAGE_SIZE;i++){
            incomes.add(new Income(i,"2022-04-" + (i+1),(long)(Math.random()*2000)));
        }
        return incomes;
    }
}
